// pages/home/hSearch/hSearch.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resultArr:[],//模糊搜索列表
    recordArr:[],//历史记录
    isSelected:true,
    searchValue:'',//搜索内容
  },

  //删除本地历史搜索
  deleteClick:function(){
    var that = this;
    if (that.data.isSelected) {
      wx.removeStorageSync('ussearch');
      that.setData({
        recordArr: wx.getStorageSync('ussearch')//店铺历史记录
      })
    } else {
      wx.removeStorageSync('uvsearch');
      that.setData({
        recordArr: wx.getStorageSync('uvsearch')//品种历史记录
      })
    }
  },

  //模糊搜索列表点击
  resultClick:function(e){
    var that = this;
    var searchType = that.data.isSelected ? '0' : '1';
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '../searchResult/searchResult?tit=' + e.currentTarget.id + '&type=' + searchType,
    })

    that.saveLocal(e.currentTarget.id);
  },

  //历史搜索点击
  recordClick: function (e) {
    var that = this;
    console.log(e.currentTarget.id);
    var searchType = that.data.isSelected ? '0' : '1';
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '../searchResult/searchResult?tit=' + e.currentTarget.id + '&type=' + searchType,
    })
    //that.saveLocal(e.currentTarget.id);
  },


  //及时输入搜索
  inputSearch:function(e){
    console.log('e.detail.value');

    console.log(e.detail.value.length);

    var that = this;
    if (e.detail.value.length == 0){
      that.setData({
        resultArr: [],//模糊搜索列表
        searchValue:'',
      })
      that.getLocal();
    }else{
      that.setData({
        searchValue: e.detail.value,
      })
      that.requestData();
    }

  },

  //输入完成点击
  searchClick:function(e){
    var that = this;
    console.log(e.detail.value);

    //that.saveLocal(e.detail.value);
  },

  //请求数据
  requestData:function(){
    var that = this;
    var url = '';
    if (that.data.isSelected){
      url = 'shop/search';
    }else{
      url = 'pro/search';
    }
    wx.request({
      url: app.globalData.requestURL + url,
      data:{
        content: that.data.searchValue,
      },
      complete:function(){

      },
      success:function(res){
        console.log(res.data);
        if(res.data.code ==0){
          that.setData({
            resultArr: res.data.resultData,
          })
          console.log(that.data.resultArr);
          console.log(that.data.searchValue.length);
          console.log(that.data.resultArr.length);
        }
      },
      fail:function(res){
        console.log(res.data);
      }
    })
  },
  


  //本地缓存
  getLocal:function(){
    //获取历史记录
    var that = this;
    that.data.recordArr = new Array;
    if (that.data.isSelected) {
      that.setData({
        recordArr: wx.getStorageSync('ussearch')//店铺历史记录
      })
    } else {
      that.setData({
        recordArr: wx.getStorageSync('uvsearch')//品种历史记录
      })
    }
  },

  //本地保存
  saveLocal:function(e){
    var that = this;
    console.log(that.data.recordArr);
    console.log('本地保存' + e);
    var testArr = that.data.recordArr;
    var newArr;
    if(testArr.length == 0){
      console.log('长度0');
      newArr = new Array;
      newArr.push(e);
    }else{
      console.log('长度!=0');
      newArr = testArr; 

      if (testArr.indexOf(e) == -1) {
        newArr.push(e);
        console.log('添加添加');
      }
    }

    if (that.data.isSelected) {
      wx.setStorageSync('ussearch', newArr);
      that.setData({
        recordArr: wx.getStorageSync('ussearch')//店铺历史记录
      })
    } else {
      wx.setStorageSync('uvsearch', newArr);
      that.setData({
        recordArr: wx.getStorageSync('uvsearch')//品种历史记录
      })
    }

    console.log('本地数据');
    console.log(that.data.recordArr);

  },

  chooseType:function(e){
    var that = this;
    console.log(e.currentTarget.id);
    if (e.currentTarget.id ==1){
      that.setData({
        isSelected:true,
      })
    }else{
      that.setData({
        isSelected: false,
      })
    }
    //如果有数据直接搜索 没有数据显示历史记录
    if (that.data.searchValue){
      that.requestData();
    }else{
      that.getLocal();
    }

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLocal();//获取本地缓存
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})